package cn.powertime.evaluation.app.controller;


import cn.powertime.evaluation.Constants;
import cn.powertime.evaluation.entity.DbqNews;
import cn.powertime.evaluation.exception.BusinessException;
import cn.powertime.evaluation.facade.DbqNewsFacade;
import cn.powertime.evaluation.service.DbqNewsService;
import cn.powertime.evaluation.vo.ParamPageVo;
import cn.powertime.evaluation.vo.ReqNewsVos;
import cn.powertime.evaluation.vo.WebReqNewsSearchVo;
import cn.powertime.evaluation.vo.WebRespNewsListVo;
import cn.powertime.evaluation.web.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 广告/新闻表 前端控制器
 * </p>
 *
 * @author liqi
 * @since 2019-09-23
 */
@RestController
@RequestMapping("/app/dbq-news")
@Api(value = "/app/dbq-news", tags = "广告/新闻/公告接口", produces = MediaType.ALL_VALUE)
public class AppDbqNewsController extends BaseController {

    @Autowired
    private DbqNewsFacade dbqNewsFacade;

    @Autowired
    private DbqNewsService dbqNewsService;

    @ApiOperation(value = "新闻列表",response = WebRespNewsListVo.class)
    @PostMapping(value = "/newsList")
    public Object list(@Validated @RequestBody ReqNewsVos vo) {
        Page page= PageHelper.startPage(vo.getPage(),vo.getSize());
        QueryWrapper<DbqNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status" , Constants.STATUS_CREDIT);
        queryWrapper.eq("type",vo.getType());
        queryWrapper.orderByDesc("create_time");
        List<DbqNews> dbqNews=dbqNewsService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(dbqNews);
        return success1(dbqNews,pageInfo.getPages(),pageInfo.getPageSize(),page.getTotal());
    }

    @ApiOperation(value = "查看详情")
    @GetMapping(value = "/details/{newsId}")
    @ApiImplicitParam(name = "newsId", value = "新闻ID", required = true, dataType = "Long")
    public Object chapterList(@PathVariable("newsId") @NotNull(message = "新闻id不能为空") Long newsId) {
        QueryWrapper<DbqNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status" , Constants.STATUS_CREDIT);
        queryWrapper.eq("type",Constants.STATUS_CREDIT);
        queryWrapper.eq("id",newsId);
        List<DbqNews> dbqNews =dbqNewsService.list(queryWrapper);
        long pv =  dbqNews.get(0).getNewsWarePv();
        pv++;
        DbqNews dbqNews1 = new DbqNews();
        dbqNews1.setNewsWarePv(pv);
        dbqNews1.setId(newsId);
        boolean b = dbqNewsService.updateById(dbqNews1);
        if(!b){
            throw new BusinessException("增加浏览量失败");
        }
        return success(dbqNews);
    }

}
